package com.ptswitch.material.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.hxzon.util.DateFormatUtil;

import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.model.contract.ContractItem;

public class ContractUtil {
    private static final Map<String, Integer> contractCodeCache = new HashMap<String, Integer>();
    private static final Map<String, Integer> contractItemCodeCache = new HashMap<String, Integer>();

    public static void buildContractCode(Contract contract) {
        String code = DateFormatUtil.format(new Date(), "'CON-'yyMMdd-");
        Integer num = contractCodeCache.get(code);
        if (num != null) {
            num += 1;
        } else {
            num = 1;
        }
        contract.setCode(code + num);
        contractCodeCache.clear();
        contractCodeCache.put(code, num);
    }

    public static void buildContractItemCode(ContractItem contractItem) {
        String code = DateFormatUtil.format(new Date(), "'S'-DDD-");
        Integer num = contractItemCodeCache.get(code);
        if (num != null) {
            num += 1;
        } else {
            num = 1;
        }
        contractItem.setCode(code + num);
        contractItemCodeCache.clear();
        contractItemCodeCache.put(code, num);
    }

}
